<template>
  <div class="a-header">
    <div class="zs-section">
      <h3>日积月累</h3>
      <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal">
        <el-menu-item
          v-for="(item, index) in menuList"
          :key="index"
          :index="item.name"
          v-show="!item.hidden"
          @click="handleOnClick(item)"
        >{{item.title}}</el-menu-item>
      </el-menu>
    </div>
    <div class="zs-section">
      <el-button v-if="!visible" @click="handleOnClickLogin">注册</el-button>
      <el-button v-if="!visible" @click="handleOnClickSignin">登录</el-button>
      <el-dropdown v-else @command="handleOnClickLogout">
        <span class="el-dropdown-link">
          {{visible.name}}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <!-- <el-button type="success">退出</el-button> -->
  </div>
</template>
<script>
export default {
  data() {
    return {
      // visible: true
    }
  },
  computed: {
    menuList() {
      let _menus = window.sessionStorage.getItem('_menus');
      return JSON.parse(_menus)[0].childrens;
    },
    activeIndex() {
      return this.$route.name;
    },
    visible() {
      let test = window.sessionStorage.getItem('token') ? window.sessionStorage.getItem('token').split('.') : false
      if (!!test) {
        let userInfo = JSON.parse(this.b64DecodeUnicode(test[1]));
        return userInfo;
      }
      return false;
    }
  },
  methods: {
    b64DecodeUnicode(str) {
      return decodeURIComponent(atob(str).split('').map(function (c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
      }).join(''));
    },
    handleSelect(key, keyPath) {
      this.$router.push({ path: keyPath });
    },
    handleOnClick(item) {
      this.$router.push({ path: `/home/${item.path}` })
    },
    handleOnClickSignin() {
      window.location.href = './signin.html'
    },
    handleOnClickLogin() {
      window.location.href = './login.html'
    },
    async handleOnClickLogout() {
      let res = await this.$http.logout();
      window.sessionStorage.removeItem('user');
      this.handleOnClickSignin();
    }
  }
}
</script>
<style lang="less" scoped>
.a-header {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  padding: 0 120px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .a-header-title {
    color: #000000;
    font-size: 1.63em;
  }
  .zs-section {
    display: flex;
    align-items: center;
  }
}
</style>
